function GetRequest() {
    var url = location.search; //获取url中"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for(var i = 0; i < strs.length; i ++) {
            theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
        }
    }
    return theRequest;
}
var Request = new Object();
Request = GetRequest();


var url = document.location.toString();
var arrUrl = url.split("//");
var start = arrUrl[1].indexOf("/");
var relUrl = arrUrl[1].substring(0,start);


function pageAlert(txt,title){
    txt = txt ||'';
    title = title ||'提示';
    var $body =$('body');
    var $alert = $body.find('#alertPanel');
    if(!$alert.length){
        var html =  '<div class="page-alert-wrapper" id="alertPanel">'+
            '<span class="page-jumbotron" id="alertJumbotron"></span>'+
            '<div class="page-alert">'+
            '<header class="page-title" id="alertTitile">提示</header>'+
            '<div class="page-content" id="alertContent">'+
            '</div>'+
            '<footer class="page-footer"  id="alertBtn">确认</footer>'+
            '</div></div>';
        $body.append(html);
        $alert = $body.find('#alertPanel');
    }

    $('#alertTitile').text(title);   //设置标题
    $('#alertContent').text(txt);    //设置中间内容
    $('#alertPanel').show();    //弹框显示
    $('#alertPanel').off().on('click','#alertBtn,#alertJumbotron',function(){
        $('#alertPanel').hide();
    })
}



// 弹窗关闭事件
$('#popupX').on('click', function () {
    $('#popup').hide();
});
// 访问接口，动态添加弹出层银行及图片
function backMask(result) {
    // 接口访问成功后的操作
    var item = '';
    // 接口返回的数据
    var result = result;
    for (var i = 0; i < result.length; i++) {
        item += '<div class="popup-item"><img class="popup-icon" src="../../img/___payment/'+result[i].bankCode+'" alt=""><p class="popup-info">'+result[i].bankName+'</p><span class="popup-next"><img src="../../img/ic_back.png" /></span></div>'
    }
    $('#popupList').append(item);
}
//添加新卡页面
/*$('.popup-footer').on('click', '.popup-item', function (e) {
    $('#popupX').click();
    //app跳转支付申请短信页面
    window.open(arrUrl[0] + "//" + relUrl + "/payment/payCupPayPage?paymentId="+strId+"&agreeid="+agreeid, "_self");
});*/



function pay(){
    var pType = $("#payButton").attr("data-type");

   if(pType==""){
        alert("请选择支付方式!")
    }
    else {

        $.ajax({
            url: "/payTL/pay",
            type: 'POST',
            async:true,
            data: {
                paymentId: Request['paymentID'],
                pType: pType,
            },
            dataType: 'json',
            contentType: "application/x-www-form-urlencoded",
            beforeSend: function () {
                console.log("正在进行，请稍候");
            },
            success: function (responseStr) {
                console.log(responseStr);
                console.log(+responseStr.data.payinfo);
                console.log(responseStr.data.paymentId);
                console.log(responseStr.data.gobackurl);
                console.log(responseStr.errorCode);
                var str = responseStr.data.payinfo;
                var strId = responseStr.data.paymentId;
                var backurl = responseStr.data.gobackurl;
                var code  = responseStr.errorCode;
                if(code == 8888){
                    //app要调转到绑卡页面，同时将paymentID传到后台，进行通联账单查询
                    window.open(arrUrl[0] + "//" + relUrl + "/payment/payCupRegPage?paymentId="+strId, "_self");
                    return false;
                }else if(code == 9999){
                    var json = responseStr.banks;
                    var agreeid;
                    backMask(json);
                    // 自动弹窗
                    $('#popup').show();
                    // 获取银行名称
                    $('#popupList').on('click', '.popup-item', function (e) {
                        var $creditCard = $('.credit-card');
                        var $info = $(this).find('.popup-info');
                        var infoTxt = $info.text();
                        $('#popupX').click();
                        $.each(json,function(j,ele){ //获取协议编码
                            if(infoTxt == ele.bankName){
                                agreeid = ele.agreeid;
                            }
                        });
                        //app跳转支付申请短信页面
                        window.open(arrUrl[0] + "//" + relUrl + "/payment/payCupPayPage?paymentId="+strId+"&agreeid="+agreeid, "_self");
                    });
                    //添加新卡
                    $('.popup-footer').on('click', '.popup-item', function (e) {
                        $('#popupX').click();
                        //app要调转到绑卡页面，同时将paymentID传到后台，进行通联账单查询
                        window.open(arrUrl[0] + "//" + relUrl + "/payment/payCupRegPage?paymentId="+strId, "_self");

                    });
                    return false;
                }
                //调用app的原生方法,APP跳转到支付宝等等
                if(checkOS()=="Android"){
                    maojia.mjToPay(str);
                } else {
                    setupWebViewJavascriptBridge(function(bridge) {
                        bridge.callHandler('mjToPay',{payUrl:str}, function(response) {
                        })
                    })
                }
                window.open(arrUrl[0] + "//" + relUrl + "/payment/payWait?paymentId="+strId+"&gobackurl="+backurl, "_self");
                //app要调转到等待页面，同时将paymentID传到后台，进行通联账单查询
            },
            error: function () {
                pageAlert("支付请求失败")
            }
        });
    }

}

//判断是android还是IOS系统
function checkOS() {
    var ua = navigator.userAgent.toLowerCase();
    if (/iphone|ipad|ipod/.test(ua)) {
        return "IOS";
    } else if (/android/.test(ua)) {
        return "Android";
    }
}
//o家APP初始ios 注册
function setupWebViewJavascriptBridge(callback) {
    if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); }
    if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); }
    window.WVJBCallbacks = [callback];
    var WVJBIframe = document.createElement('iframe');
    WVJBIframe.style.display = 'none';
    WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
    document.documentElement.appendChild(WVJBIframe);
    setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0)
}
//ios调用
setupWebViewJavascriptBridge(function(bridge) {
    bridge.registerHandler('testJSFunction', function(data, responseCallback) {
        responseCallback('js执行过了');
    })
})
//返回上一页
function returnBack(){
    //调用原生的返回上一页方法
    if(checkOS()=="Android")
    {
        maojia.mjGoBack();
    } else{
    setupWebViewJavascriptBridge(function(bridge) {
        bridge.callHandler('mjGoBack', function(response) {
        })
    })
    }

}

//点击改变选择状态  当前按钮的对象 需要切换图片的地址
function changeState(obj,src1,src2) {
    if(obj.attr("src") == src1){
        obj.attr("src",src2);
        return 'on';
    }else if(obj.attr("src") == src2){
        obj.attr("src",src1);
        return 'off';
    }
}

//点击改变选择状态  当前按钮的对象 需要切换图片的地址
$("#checkOnWechat img").click(function(){
    var status = changeState($("#checkOnWechat img"),'../../../img/Checkbox_off_light.png','../../../img/Checkbox_on.png');
    if(status=='on'){
        $("#payButton").attr("data-type","W03");

        $("#checkOnLian img").attr("src",'../../../img/Checkbox_off_light.png');
        $("#checkOnZhi img").attr("src",'../../../img/Checkbox_off_light.png');
    }else if(status=='off'){
        $("#checkOnWechat img").attr("data-status","off");
        $("#payButton").attr("data-type","");
    }
});

$("#checkOnZhi img").click(function(){
    var status = changeState($("#checkOnZhi img"),'../../../img/Checkbox_off_light.png','../../../img/Checkbox_on.png');
    if(status=='on'){
        //用来判断支付类型的一个自定义的属性
        $("#payButton").attr("data-type","A01");
        $("#checkOnWechat img").attr("src",'../../../img/Checkbox_off_light.png');
        $("#checkOnLian img").attr("src",'../../../img/Checkbox_off_light.png');
    }else if(status=='off'){
        $("#checkOnZhi img").attr("data-status","off");
        $("#payButton").attr("data-type","");
    }
});

$("#checkOnLian img").click(function(){
    var status = changeState($("#checkOnLian img"),'../../../img/Checkbox_off_light.png','../../../img/Checkbox_on.png');
    if(status=='on'){
        $("#payButton").attr("data-type","A03");
        $("#checkOnWechat img").attr("src",'../../../img/Checkbox_off_light.png');
        $("#checkOnZhi img").attr("src",'../../../img/Checkbox_off_light.png');
    }else if(status == 'off'){
        $("#checkOnLian img").attr("data-status","off");
        $("#payButton").attr("data-type","");
    }
});





